package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.dao.PersonDao;
import com.gxuwz.medical.dao.ProveDao;
import com.gxuwz.medical.domain.prove.Prove;
import com.gxuwz.medical.vo.PageBean;

public class ProveServlet extends BaseServlet{


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		try {
			if(method.equals("list")){
				list(request,response);
			}else if(method.equals("swich")){
				swich(request,response);
			}else if(method.equals("serch")){
				serch(request,response);
			}else if(method.equals("toadd")){
				toadd(request,response);
			}else if(method.equals("del")){
				del(request,response);
			}else if(method.equals("add")){
				add(request,response);
			}else if(method.equals("edit")){
				edit(request,response);
			}
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/prove/prove_list.jsp";
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = new ProveDao().queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		ProveDao dao = new ProveDao();
		String proveno = request.getParameter("proveno");
		dao.del(proveno);
		list(request, response);
	}
	private void swich(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/prove/prove_add_swich.jsp";
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = new PersonDao().queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void serch(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException {
		// TODO Auto-generated method stub
		String path = "/page/prove/prove_add_swich.jsp";
		PersonDao dao = new PersonDao();
		String page = request.getParameter("page");
		String fullname = request.getParameter("fullname");
		fullname = new String(fullname.getBytes("ISO-8859-1"),"UTF-8");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = dao.queryPersonByName(fullname, currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException, ParseException {
		// TODO Auto-generated method stub
		String ybcardno = request.getParameter("ybcardno");
		String idcard = request.getParameter("idcard");
		String illname = request.getParameter("illname");
		String timestr = request.getParameter("time");
		int time = Integer.parseInt(timestr);
		Date stime = new Date();
		Date etime = new Date();
		etime.setYear(etime.getYear()+time);
		Prove  p=  new Prove(ybcardno,idcard,illname,stime,etime);
		new ProveDao().add(p);
		list(request, response);
	}
	private void toadd(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException, ParseException {
		// TODO Auto-generated method stub
		String path="/page/prove/prove_add.jsp";
		String ybcardno = request.getParameter("ybcardno");
		if(new Prove().hasProve(ybcardno)){
			request.setAttribute("msg", "慢病证信息已存在");
			list(request, response);
			return;
		}
		process(request, response,path);
	}

	
	private void edit(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException, ParseException {
		// TODO Auto-generated method stub
		String proveno = request.getParameter("proveno");
		String ybcardno = request.getParameter("ybcardno");
		String idcard = request.getParameter("idcard");
		String illname = request.getParameter("illname");
		String stimestr = request.getParameter("stime");
		String etimestr = request.getParameter("etime");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date stime = df.parse(stimestr);
		Date etime = df.parse(etimestr);
		Prove  p=  new Prove(proveno,ybcardno,idcard,illname,stime,etime);
		new ProveDao().edit(p);
		list(request, response);
	}
}
